package cn.gdcp.servicebestpractice;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
      private   Button startBtn;
      private  Button pauseBtn;
      private   Button cancelBtn;
      private DownloadService.DownloadBinder downloadBinder;
      private ServiceConnection connection=new ServiceConnection() {
          public void onServiceConnected(ComponentName name, IBinder service) {
              downloadBinder=(DownloadService.DownloadBinder) service;
          }

          public void onServiceDisconnected(ComponentName name) {

          }
      };
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initButtonView();
        startDownloadService();

    }

    private void startDownloadService() {
        Intent intent=new Intent(this,DownloadService.class);
        startService(intent);
        bindService(intent,connection,BIND_AUTO_CREATE);
        if ((ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE))!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }
    }

    private void initButtonView() {
        startBtn=findViewById(R.id.start_download_button);
        pauseBtn=findViewById(R.id.pause_download_button);
       cancelBtn=findViewById(R.id.cancel_download_button);
        startBtn.setOnClickListener(this);
        pauseBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()){
            case R.id.start_download_button:
                String url="";
                downloadBinder.startDownload(url);
                break;
            case R.id.pause_download_button:
                downloadBinder.pauseDownload();
                break;
            case R.id.cancel_download_button:
                downloadBinder.cancelDownload();
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
       switch (requestCode){
           case 1:
               if (grantResults.length>0&&grantResults[0]!=PackageManager.PERMISSION_GRANTED){
                   Toast.makeText(this,"拒绝权限",Toast.LENGTH_SHORT).show();
               }
               break;
           default:
               break;
       }
    }

    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }
}
